package com.chm.fw.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * listView列表加载页面
 * Created by chenmin on 2016/8/4.
 */
public abstract class BaseListViewAdapter extends BaseAdapter {
    private List<?> data;
    private LayoutInflater inflater;
    private int layoutId;
    private int p;
    private ViewGroup parent_v;
    private ViewGroup root;
    private boolean parent = true;

    public BaseListViewAdapter(List<?> data, Context context, int layoutId) {
        this.data = data;
        this.inflater = LayoutInflater.from(context);
        this.layoutId = layoutId;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void notifyDataSetChanged(List<?> data) {
        this.data = data;
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        this.parent_v = parent;
        if (convertView == null) {
            if (this.parent) {
                convertView = inflater.inflate(layoutId, null);
            } else {
                convertView = inflater.inflate(layoutId, root, false);
            }
        }
        ViewHolder vh = ViewHolder.getViewHolder(convertView);
        Object item = getItem(position);

        p = position;
        setData(vh, item);

        return convertView;
    }

    public ViewGroup getParent() {
        return parent_v;
    }

    public void setParent(boolean parent) {
        this.parent = parent;
    }

    public void setRoot(ViewGroup root) {
        this.root = root;
    }

    /**
     * 设置item数据
     */
    public abstract void setData(ViewHolder vh, Object itemData);

    public int getPosition() {
        return p;
    }

    public static class ViewHolder {
        private SparseArray<View> viewHolder;
        private View view;

        static ViewHolder getViewHolder(View view) {
            ViewHolder viewHolder = (ViewHolder) view.getTag();
            if (viewHolder == null) {
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            }
            return viewHolder;
        }

        private ViewHolder(View view) {
            this.view = view;
            viewHolder = new SparseArray<>();
            view.setTag(viewHolder);
        }

        public View get(int id) {
            View childView = viewHolder.get(id);
            if (childView == null) {
                childView = view.findViewById(id);
                viewHolder.put(id, childView);
            }
            return childView;
        }

        public View getConvertView() {
            return view;
        }

        public TextView getTextView(int id) {
            return (TextView) get(id);
        }

        public Button getButton(int id) {
            return (Button) get(id);
        }

        public ImageView getImageView(int id) {
            return (ImageView) get(id);
        }
    }
}
